package com.liveinstars.common;

import java.text.DecimalFormat;

public class StrUtil {

	//过滤特殊字符
    public static String escapeExprSpecialWord(String keyword) {
        String[] fbsArr = {"-",",","%"," "};
        for (String key : fbsArr) {
            if (keyword.contains(key)) {
                keyword = keyword.replace(key, "");
            }
        }
        return keyword;
    }
    
    public static String getDecimalPoint(Object ope) {
    	//保留小数点后四位
    	DecimalFormat decimalFormat=new DecimalFormat("0.0000");
    	return decimalFormat.format(ope);
    }

    public static String cutByMaxByteLength(String old, int maxByteLength) {
        byte[] utf8 = old.getBytes();
        if (utf8.length < maxByteLength) maxByteLength = utf8.length;
        int n16 = 0;
        int advance = 1;
        int i = 0;
        while (i < maxByteLength) {
            advance = 1;
            if ((utf8[i] & 0x80) == 0) i += 1;
            else if ((utf8[i] & 0xE0) == 0xC0) i += 2;
            else if ((utf8[i] & 0xF0) == 0xE0) i += 3;
            else { i += 4; advance = 2; }
            if (i <= maxByteLength) n16 += advance;
        }
        return old.substring(0,n16);
    }

    //蛇形转驼峰
    public static String toHumpStr(String orderField) {
        String[] strs = orderField.split("_");
        StringBuilder builder = new StringBuilder();
        for (String str : strs) {
            if (builder.length() == 0) {
                builder.append(str);
            } else {
                builder.append(str.substring(0, 1).toUpperCase());
                builder.append(str.substring(1));
            }
        }
        return builder.toString();
    }
    
    public static boolean isNumber(String str) {
    	return str.matches("-?[0-9]+.*[0-9]*");
    }
}
